<?php


namespace app\admin\model;


use think\Db;
use think\Model;

class Notice extends Model
{
    protected $table = 'xm_notice';

    public static function getListInfo($page, $limit, $data)
    {
        $where = [];
        //活动名称搜索
        if (!empty($data['title'])) {
            $where['title'] = ['like', '%' . rtrim($data['title']) . '%'];
        }
        $start_time = strtotime($data['start_time']);
        $end_time = strtotime($data['end_time'] . "23:59:59");
        if (!empty($data['end_time'])) {
            $where['add_time'] = ['between', [$start_time, $end_time]];
        }
        if (is_numeric($data['is_top'])) {
            $where['is_top'] = $data['is_top'];
        }
        if (is_numeric($data['driver_type'])) {
            $where['driver_type'] = ['like', "%" . $data['driver_type'] . "%"];
        }
        if (is_numeric($data['target'])) {
            $where['target'] = ['like', "%" . $data['target'] . "%"];
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('notice')->where($where)->count();
        $list = Db::name('notice')->where($where)->limit($start, $limit)->order('id', 'desc')->select();
        $regionList = $adminInfo = $info = [];
        foreach ($list as $key => $value) {
            $list[$key]['add_time'] = !empty($value['add_time']) ? date("Y-m-d", $value['add_time']) : '';//活动开始时间
            $list[$key]['create_time'] = !empty($value['create_time']) ? date("Y-m-d H:i:s", $value['create_time']) : '';
            $value['is_top'] == 0 ? $list[$key]['is_top'] = "否" : $list[$key]['is_top'] = "是";
            $driver_type = explode(',', $value['driver_type']);
            $str = "";
            foreach ($driver_type as $k => $v) {
                if ($v == 1) {
                    $v = "快车";
                } else {
                    $v = "城际";
                }
                $str .= $v . ";";
            }
            $list[$key]['driver_type'] = $str;
            $target = explode(',', $value['target']);
            $str = "";
            foreach ($target as $k => $v) {
                if (is_numeric($v)) {
                    switch ($v) {
                        case 0:
                            $v = "未认证";
                            break;
                        case 1:
                            $v = "已认证";
                            break;
                        case 2:
                            $v = "审核中";
                            break;
                        case 3:
                            $v = "未通过";
                            break;
                        case 4:
                            $v = "特定用户";
                            break;
                    }
                    $str .= $v . ";";
                }
            }
            $list[$key]['target'] = $str;
            //省份
            if (!empty($value['citys'])) {
                $ci = json_decode($value['citys'], true);
                $shi = $ci[3];
                $pr = $ci[2];
                $regionList = Db::name('region')->where(['region_id' => ['in', $pr]])->field('region_name')->select();
                $region_name = '';
                foreach ($regionList as $vml) {
                    $region_name .= $vml['region_name'] . ',';
                }
                $list[$key]['region_name'] = rtrim($region_name, ',');

                $shis = [];
                $prs = explode(',', $pr);
                if ($prs) {
                    foreach ($prs as $i=>$vc) {
                        $shis[$i] = $vc;
                    }
                } else {
                    foreach ($shi as $i=>$vc) {
                        $shis = $vc;
                    }
                }
            $shiList = Db::name('region')->where(['region_id' => ['in', $shis]])->field('region_name')->select();
            $region_city = '';
            foreach ($shiList as $vc) {
                $region_city .= $vc['region_name'] . ',';
            }
            $list[$key]['region_city'] = rtrim($region_city, ',');
        }
    else {
        $list[$key]['region_name'] = '全国';
        $list[$key]['region_city'] = '全国';
    }
        //创建人
        $adminInfo = getAdminInfo($value['admin_id']);
        $list[$key]['admin_name'] = $adminInfo['user_name'];

    }

return array(
'code' => 0,
'msg' => '',
'count' => $count,
'data' => $list
);
}
}